<template>
  <div style="height: 100%; width: 100%">
    <div ref="basicCanvas" style="height: 100%; width: 100%"></div>
  </div>
</template>

<script>
import { resize } from "./resize"
export default {
  mixins: [resize],
  data() {
    return {
      myChart: null,
      xAxisData: [],
      barChartData: [],
      lineChartData: [],
    };
  },
  props: {
    chartData: {
      type: Array,
      default: () => [],
    },
    title: {
      type: Array,
      default: () => ["同比增长率(%)", "时序进度(%)"],
    },
  },
  watch: {
    chartData: {
      handler(val){
        this.initChart(val);
      },
      deep: true,
    },
  },
  created(){
    this.initChart(this.chartData);
  },
  methods: {
    async initChart(val) {
      await this.$nextTick();
      this.xAxisData = [];
      this.barChartData = [];
      this.lineChartData = [],
      val.forEach(it => {
        this.xAxisData.push(it.areaName);
        this.barChartData.push(it.tbYearInrease);
        this.lineChartData.push(it.completeYearBudget);
      });

      if(this.myChart) { this.myChart.dispose() };
      this.myChart = this.$echarts.init(this.$refs.basicCanvas);

      let lineMin = Math.min(...this.lineChartData),
          lineMax = Math.max(...this.lineChartData);

      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          // formatter: "{b} <br /> {a0}: {c0}" + "%" + "<br />{a1}: {c1}" + "%",
        },
        legend: {
          data: this.title,
        },
        grid: {
          left: "20px",
          right: "20px",
          top: "60px",
          bottom: "80px",
        },
        xAxis: {
          type: "category",
          data: this.xAxisData,
          axisLabel: {
            // interval: 0,
            // rotate: 33,
            fontSize: "12px",
            formatter(val){
              return val.split("").join("\n");
            }
          },
        },
        yAxis: [
          {
            type: "value",
            splitLine: { show: false }, //去除网格线
            axisLabel: {
              formatter: "{value}%",
              show: false,
            },
            axisTick: {
              //y轴刻度线
              show: false,
            },
            axisLine: {
              //y轴
              show: false,
            },
          },
          {
            type: "value",
            min: lineMin / 1.02 - 5,
            max: lineMax,
            splitLine: { show: false }, //去除网格线
            axisLabel: {
              formatter: "{value}%",
              show: false,
            },
            axisTick: {
              //y轴刻度线
              show: false,
            },
            axisLine: {
              //y轴
              show: false,
            },
          }
        ],

        series: [
          {
            type: "bar",
            name: this.title[0],
            data: this.barChartData,
            yAxisIndex: 0,
            barWidth: 22,
            color: "rgb(222,235,247)",
            borderWidth: 1,
            itemStyle: {
              normal: {
                borderColor: 'rgb(131,161,216)',
                borderWidth: 1,
                label: {
                  show: true,
                  position: "bottom",
                  textStyle: {
                    color: "#000",
                  },
                  formatter: "{c}",
                  offset: [0, -25],
                },
              },
            },
          },
          {
            type: "line",
            name: this.title[1],
            smooth: true,
            yAxisIndex: 1,
            data: this.lineChartData,
            lineStyle: {
              normal: {
                color: "#c00000", // 线条颜色
              },
            },
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: "top",
                  textStyle: {
                    color: "#c00000",
                  },
                  formatter: "{c}",
                },
              },
            },
          },
        ],
      };
      this.myChart.setOption(option);
    },
  },
};
</script>

<style>
</style>
